<?php

$user_id = $GLOBALS['user_id'];

$user = new user($user_id);
$user_info[$user_id] = $user->load_tables('user_settings');

//echo government_setting('treasury_name');

// echo '<pre>';
// echo var_dump($user_info);
// echo '</pre>';

echo 'Welcome to the Voting Area.<br><Br>';


if (government_setting('elections_running') == 'yes')
{
	switch (@$_REQUEST['function'])
	{
		case 'confirm':
			# log vote and thank user
			if (strlen(@$_REQUEST['v1']) > 0 && is_numeric(@$_REQUEST['v1']))
			{
				$player_id = $_REQUEST['v1'];
			}
			else 
			{
				$player_id = false;
			}
			$sql = "INSERT INTO `user_votes` 
			( `assoc_id` , `voted_for` , `ip_adr` , `time` )
			VALUES (
			'$user_id', '$player_id', '0', '0'
			)";
			$vote_status = $db->query($sql);
			
			if ($vote_status)
			{
				echo 'Thank you very much for voting.';
			}
			else 
			{
				echo 'Voting error. You have probably already voted.';
			}
			break;
			
			
			
		case 'ask':
			# ask if player is sure they want to vote this person
			if (strlen(@$_REQUEST['v1']) > 0 && is_numeric(@$_REQUEST['v1']))
			{
				$player_id = $_REQUEST['v1'];
			}
			else 
			{
				$player_id = false;
			}
		
			$query = "SELECT * FROM `user_settings` WHERE `assoc_id` = '$player_id'";
			
			
			if ($result = $db->query($query)) {
			    
			    $num_results = $result->num_rows;
			    if ($num_results == 1)
			    {
			        echo '<h2>Please confirm your vote...</h2><p>';
			        while ($row = $result->fetch_assoc()) {
			            $lookup_id = $row['assoc_id'];
           				$user_lookup = new user($lookup_id);
			            $user_info[$lookup_id] = $user_lookup->load_tables('user_stats',1);
			            print '<li>'. $user_lookup->select_user($user_info[$lookup_id],"/vote/confirm/$player_id/","Yes! I would like to vote for ");
			            print '<li>'.ahref('/vote/','No thanks, please escore me back to the main voting area!');
			        }
			    }
			    else
			    {
			        echo '<h1>Player Search <small>('.$num_results.' results)</small></h1><p>';
			        echo 'No players found!';
			    }
			    /* free result set */
			    $result->close();
			}
				break;
		

			
			
			
			
			case 'search':
			# display results

				$term = $_REQUEST['q'];
				
				// check if the search is for a players username or id number
				if (is_numeric($term)) 
				{ 
				    $query = "SELECT * FROM `user_settings` WHERE `assoc_id` = '$term'";}
				else
				{
				    $query = "SELECT * FROM `user_settings` WHERE `username` LIKE '%$term%' OR `prefix` LIKE '%$term%' OR `postfix` LIKE '%$term%'";    
				}
				
				if ($result = $db->query($query)) {
				    
				    $num_results = $result->num_rows;
				    if ($num_results > 0)
				    {
				        if (strlen($term) > 0)
				        {
				            echo '<h2>Player Search <small>('.$num_results.' results)</small></h2><p>';
				        }
				        else
				        {
				            echo '<h2>All possible candidates ('.$num_results.')</h2><p>';
				        }
				        $player_info = array();
				        /* fetch associative array */
				        while ($row = $result->fetch_assoc()) {
				             //printf ("<li>%s %s %s (%s)</li>\n", $row["prefix"], $row["username"],$row["postfix"],$row["status"]);
				             $player_id = $row['assoc_id'];
				             $player_info["/vote/ask/$player_id/"] =  $row['prefix'] .' '. $row['username'] . ' ' .  $row['postfix'] . ' (' . $row['status'] . ')';
				        }
				        echo player_options($player_info);
				    }
				    else
				    {
				        echo '<h1>Player Search <small>('.$num_results.' results)</small></h1><p>';
				        echo 'No players found!';
				    }
				    /* free result set */
				    $result->close();
				}
			
			break;
		default:
			echo 'Elections are running. Please enter the name of person you would like to vote for or pick from the list below...<BR><BR>';
			echo html_form('/vote/search/',"Name: <input name=\"q\"><input value=\"Search\" type=\"submit\">");
			break;
	}
}
else 
{
	echo 'Elections are not running at the moment, please come back later.';
}



//$options = array();
//$options['/home/'] = 'Go Home';
//$options['/forum/'] = 'Go to the Forum';
//$options['/vote/'] = 'Go to the Voting Booth';
//$options['/alps/'] = 'Wait in line to go to the alps <small>(reqs ticket)</small>';
//echo '<p>';
//echo player_options($options);
//echo '</p>';
//
//



?>